package com.itheima.mqtt.client;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class MessageCallback implements MqttCallback {

    private static final Logger LOG = LoggerFactory.getLogger(MessageCallback.class);

    /**
     * 丢失了对服务端的连接后出发的回调
     * @param throwable
     */
    @Override
    public void connectionLost(Throwable throwable) {
        // 资源的清理 重连
        LOG.info("丢失了对服务端的连接");
    }

    /**
     * 应用受到消息后出发的回调
     * @param topic
     * @param message
     * @throws Exception
     */
    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        LOG.info("订阅者订阅到了消息，topic={},messageid={},qos={},payload={}" + topic,
                message.getId(),
                message.getQos(),
                new String(message.getPayload()));
    }

    /**
     * 消息已被送达服务器后出发的回调
     * @param token
     */
    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        LOG.info("消息已被送达服务器，token={}", token);
    }
}
